<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      size="small"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          :disabled="dataForm.id !== 0"
          v-model="dataForm.username"
          placeholder="登录帐号"
        ></el-input>
      </el-form-item>
      <el-form-item label="用户姓名" prop="nickName">
        <el-input v-model="dataForm.nickName" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="用户邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
        <el-tag type="warning">账号的登录密码默认为：手机号的的后六位</el-tag>
      </el-form-item>
      <el-form-item label="用户头像">
        <el-input placeholder="头像"></el-input>
      </el-form-item>

      <el-form-item label="用户状态" size="mini" prop="dataState">
        <el-radio-group v-model="dataForm.dataState">
          <el-radio :label="0">正常</el-radio>
          <el-radio :label="-1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="用户角色" size="mini">
        <el-checkbox-group
          v-model="dataForm.roleIds"
          @change="handleCheckedRoleIdsChange"
        >
          <el-checkbox
            v-for="role in roleList"
            :key="role.id"
            :label="role.id"
            >{{ role.name }}</el-checkbox
          >
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from "@/utils/validate";
import { roleList } from "@/api/role";
import { saveAndUpdate } from "@/api/user";
export default {
  data() {
    var validateEmail = (rule, value, callback) => {
      if (!isEmail(value)) {
        callback(new Error("邮箱格式错误"));
      } else {
        callback();
      }
    };
    var validateMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error("手机号格式错误"));
      } else {
        callback();
      }
    };
    return {
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        username: "",
        nickName: "",
        email: "",
        phone: "",
        roleIds: [],
        dataState: 0,
        unitId: 0,
      },
      dataRule: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },
          { validator: validateEmail, trigger: "blur" },
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          { validator: validateMobile, trigger: "blur" },
        ],
        nickName: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    setUnitId(unitId) {
      this.dataForm.unitId = unitId;
    },
    init(data) {
      this.dataForm.id = 0;
      if (data) {
        this.dataForm.id = data.id || 0;
      }
      roleList({})
        .then((res) => {
          if (res && res.code === 0) {
            this.roleList = res.data;
          } else {
            this.roleList = [];
          }
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        })
        .then(() => {
          if (this.dataForm.id) {
            this.dataForm.id = data.id;
            this.dataForm.username = data.username;
            this.dataForm.nickName = data.nickName;
            this.dataForm.email = data.email;
            this.dataForm.phone = data.phone;
            this.dataForm.roleIds = data.roleIds || [];
            this.dataForm.dataState = data.dataState;
            this.dataForm.unitId = data.unitId;
          }
        });
    },
    handleCheckedRoleIdsChange(value) {
      let checkedCount = value.length;
      debugger;
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          saveAndUpdate({
            id: this.dataForm.id || "",
            username: this.dataForm.username,
            nickName: this.dataForm.nickName,
            email: this.dataForm.email,
            phone: this.dataForm.phone,
            dataState: this.dataForm.dataState,
            roleIds: this.dataForm.roleIds || [],
            unitId: this.dataForm.unitId,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
